<template>
  <a-modal
    v-model:visible="visible"
    :title="title"
    @ok="saveTemp"
    @close="closeModal"
  >
    <a-form :model="form">
      <a-form-item label="入缸时间" validate-trigger="input" required>
        <a-date-picker
          style="width: 100%"
          v-model="form.start_time"
          placeholder="请选择入缸时间"
        />
      </a-form-item>
      <a-form-item label="入温" validate-trigger="input" required>
        <a-input
          style="width: 100%"
          v-model="form.enter_temp"
          placeholder="请输入入温"
        />
      </a-form-item>
      <a-form-item label="地温" validate-trigger="input" required>
        <a-input
          style="width: 100%"
          v-model="form.group_temp"
          placeholder="请输入地温"
        />
      </a-form-item>
      <a-form-item label="一对时">
        <a-input
          style="width: 100%"
          v-model="form.one"
          placeholder="请输入一对时"
        />
      </a-form-item>
      <a-form-item label="二对时">
        <a-input
          style="width: 100%"
          v-model="form.two"
          placeholder="请输入二对时"
        />
      </a-form-item>
      <a-form-item label="三对时">
        <a-input
          style="width: 100%"
          v-model="form.three"
          placeholder="请输入三对时"
        />
      </a-form-item>
      <a-form-item label="四对时">
        <a-input
          style="width: 100%"
          v-model="form.four"
          placeholder="请输入四对时"
        />
      </a-form-item>
      <a-form-item label="五对时">
        <a-input
          style="width: 100%"
          v-model="form.five"
          placeholder="请输入五对时"
        />
      </a-form-item>
      <a-form-item label="六对时">
        <a-input
          style="width: 100%"
          v-model="form.six"
          placeholder="请输入六对时"
        />
      </a-form-item>
      <a-form-item label="七对时">
        <a-input
          style="width: 100%"
          v-model="form.seven"
          placeholder="请输入七对时"
        />
      </a-form-item>
      <a-form-item label="八对时">
        <a-input
          style="width: 100%"
          v-model="form.eight"
          placeholder="请输入八对时"
        />
      </a-form-item>
      <a-form-item label="九对时">
        <a-input
          style="width: 100%"
          v-model="form.nine"
          placeholder="请输入九对时"
        />
      </a-form-item>
      <a-form-item label="十对时">
        <a-input
          style="width: 100%"
          v-model="form.ten"
          placeholder="请输入十对时"
        />
      </a-form-item>
      <a-form-item label="十一对时">
        <a-input
          style="width: 100%"
          v-model="form.eleven"
          placeholder="请输入十一对时"
        />
      </a-form-item>
      <a-form-item label="十二对时">
        <a-input
          style="width: 100%"
          v-model="form.twelve"
          placeholder="请输入十二对时"
        />
      </a-form-item>
      <a-form-item label="十三对时">
        <a-input
          style="width: 100%"
          v-model="form.thirteen"
          placeholder="请输入十三对时"
        />
      </a-form-item>
      <a-form-item label="十四对时">
        <a-input
          style="width: 100%"
          v-model="form.fourteen"
          placeholder="请输入十四对时"
        />
      </a-form-item>
      <a-form-item label="十五对时">
        <a-input
          style="width: 100%"
          v-model="form.fifteen"
          placeholder="请输入十五对时"
        />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
import { saveTempData } from '@/api/produce';
import { Message } from '@arco-design/web-vue';
import { ref } from 'vue';
const props = defineProps({
  gettempInfo: {
    type: Function,
    default: () => {},
  },
});
const visible = ref(false);
const title = ref('新增');
const form = ref({
  start_time: '',
  enter_temp: '',
  group_temp: '',
  one: '',
  two: '',
  three: '',
  four: '',
  five: '',
  six: '',
  seven: '',
  eight: '',
  nine: '',
  ten: '',
  eleven: '',
  twelve: '',
  thirteen: '',
  fourteen: '',
  fifteen: '',
});
const clickTemp = ref(null);
const openMoadl = (record: any, type) => {
  if (record.id && type === 'edit') {
    const properties = [
      'one',
      'two',
      'three',
      'four',
      'five',
      'six',
      'seven',
      'eight',
      'nine',
      'eleven',
      'twelve',
      'thirteen',
      'fourteen',
      'fifteen',
    ];
    for (let i = 0; i < properties.length; i++) {
      form.value[properties[i]] = record[properties[i]];
    }
    form.value.start_time = record.start_time;
    form.value.enter_temp = record.enter_temp;
    form.value.group_temp = record.ground_temp;
  }
  clickTemp.value = record;
  visible.value = true;
};
const saveTemp = async () => {
  const saveTempDataInfo = {
    group_id: clickTemp.value.group_id,
    redordId: clickTemp.value.redordId,
    workshop_id: clickTemp.value.workshop_id,
    start_time: form.value.start_time, // 开始时间
    enter_temp: form.value.enter_temp,
    ground_temp: form.value.group_temp,
    temp_str: '', // 温度数组
  };
  if (clickTemp.value.id) {
    saveTempDataInfo.id = clickTemp.value.id;
  }
  const nowTemp = [];
  Object.entries(form.value).forEach(([key, value]) => {
    if (key !== 'start_time' && key !== 'enter_temp' && key !== 'group_temp') {
      nowTemp.push(value);
    }
  });
  saveTempDataInfo.temp_str = JSON.stringify(nowTemp);
  const { data, code, message } = await saveTempData(saveTempDataInfo);
  Message.success(message);
  if (code === 200) {
    props.gettempInfo();
  }
};
// 关闭对话框
const closeModal = () => {
  clickTemp.value = null;
  Object.entries(form.value).forEach(([key, value]) => {
    form.value[key] = '';
  });
  console.log('关闭');
};

defineExpose({
  openMoadl,
});
</script>